#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>

#ifdef CYGWIN
#include <cygwin/in.h>
#endif

#include <rpc/rpc.h>

#include "xdr.h"
#include "ndmp.h"
#include "err.h"

char *ndmp_error_str[31] = {
        "NDMP_NO_ERR",
        "NDMP_NOT_SUPPORTED_ERR",
        "NDMP_DEVICE_BUSY_ERR",
        "NDMP_DEVICE_OPENED_ERR",
        "NDMP_NOT_AUTHORIZED_ERR",
        "NDMP_PERMISSION_ERR",
        "NDMP_DEV_NOT_OPEN_ERR",
        "NDMP_IO_ERR",
        "NDMP_TIMEOUT_ERR",
        "NDMP_ILLEGAL_ARGS_ERR",
        "NDMP_NO_TAPE_LOADED_ERR",
        "NDMP_WRITE_PROTECT_ERR",
        "NDMP_EOF_ERR",
        "NDMP_EOM_ERR",
        "NDMP_FILE_NOT_FOUND_ERR",
        "NDMP_BAD_FILE_ERR",
        "NDMP_NO_DEVICE_ERR",
        "NDMP_NO_BUS_ERR",
        "NDMP_XDR_DECODE_ERR",
        "NDMP_ILLEGAL_STATE_ERR",
        "NDMP_UNDEFINED_ERR",
        "NDMP_XDR_ENCODE_ERR",
        "NDMP_NO_MEM_ERR",
        "NDMP_CONNECT_ERR",
        "NDMP_SEQUENCE_NUM_ERR",
        "NDMP_READ_IN_PROGRESS_ERR",
        "NDMP_PRECONDITION_ERR ",
        "NDMP_CLASS_NOT_SUPPORTED_ERR",
        "NDMP_VERSION_NOT_SUPPORTED_ERR",
        "NDMP_EXT_DUPL_CLASSES_ERR ",
        "NDMP_EXT_DANDN_ILLEGAL_ERR"
};

char *scsi_error[32] = {
        "GOOD",
        "CHECK_CONDITION",
        "CONDITION_MET",
        "BUSY",
        " ",
        " ",
        " ",
        "INTERMEDIATE",
        " ",
        "INTERMEDIDATE_CONDITION_MET",
        " ",
        "RESERVATION_CONFLICT",
        " ",
        " ",
        " ",
        " ",
        "COMMAND_TERMINIATED",
        " ",
        " ",
        "QUEUE_FULL",
        " ",
        " ",
        " ",
        "ACA_ACTIVE",
        " ",
        " ",
        " ",
        " ",
        " ",
        " ",
        " ",
        "TASK_ABORTED"
};

char *errorp(int error_code)
{

char *net_error[4] = {
        "GOOD",
        "Address Lookup Error",
        "Unable to Connect to the IP address mentioned",
        "Socket Creation Error"
};

char *gen_error[10] = {
        "OK",
        " ",
        "Server did not Respond to Connection",
        "Server Rejected the NDMP Connection",
        " ",
        "XDR Error, Most likely the TCP Connection timed out or connection Broken",

        "History File Open Error",

        "History Entries exceeds 256",

        "Invalid History Variables",

        "Data Server state is not CONNECTED"
};

char *status_error[4] = {
        "Operation not permitted in this state, State is TCP Connected",
        "Operation not permitted in this state, State is Connection Accepted by Server",
        "Operation not permitted in this state, State is Authenticated",
        "Operation not permitted in this state, State is Device Open"
};


        static char msg[256];

        if ( error_code == 0xEF ) {
                strcpy(msg, "No connection exists for that operation");
                return msg;
        }

        if ( error_code == 0  ) {
                strcpy(msg,"OK");
                return msg;
        }

        if ( error_code < P_E ) {
                strcpy(msg,gen_error[error_code]);
                sprintf(msg, "%s",msg);
                return msg;
        }

        if ( error_code < S_E ) {
                strcpy(msg, "NDMP Error :");
                if ( error_code - P_E <= 30 ) strcat(msg,ndmp_error_str[ (error_code - P_E) ]);
                sprintf(msg, "%s",msg);
                return msg;
        }

        if ( error_code < N_E ) {
                strcpy(msg, "SCSI Error :");
                if ( (error_code - S_E) / 2 <= 31 ) strcat(msg,scsi_error[ (error_code - S_E) / 2 ]);
                sprintf(msg, "%s",msg);
                return msg;
        }

        if ( error_code <  STAT ) {
                strcpy(msg, "Network Error :");
                strcat(msg,net_error[ (error_code - N_E) ]);
                sprintf(msg, "%s",msg);
                return msg;
        }

        strcpy(msg, "Status Error :");
        strcat(msg,status_error[ (error_code - STAT) ]);
        sprintf(msg, "%s",msg);
        return msg;


}
